package chapter5.dao.impl;

import chapter5.entity.Admin;
import chapter5.dao.AdminDao;
import chapter5.entity.BaseDao_1;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl extends BaseDao_1 implements AdminDao {
    @Override
    public int Xz(Admin admin) {
        int count=0;
        String sql="insert into enter(name,pwd) values(?,?)";
        Object[] params={admin.getName(),admin.getPwd()};
        count=super.executeUpdate(sql,params);
        if(count>0){
            System.out.println("新增成功");
        }else{
            System.out.println("新增失败");
        }
        return count;
    }

    @Override
    public int login(Admin admin) {
        ResultSet rs=null;
        int count=0;
        try {
            String sql="select * from enter where name=? and pwd=?";
            Object[] params={admin.getName(),admin.getPwd()};
            rs=super.executeQuery(sql,params);
            if(rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(rs,null,con);
        }
        return count;
    }

    @Override
    public List<Admin> selct() {
        List<Admin> list=new ArrayList<>();
        try {
            String sql="select * from enter;";
            ResultSet rs=executeQuery(sql,null);
            while(rs.next()){
                Admin admin=new Admin();
                admin.setId(rs.getInt(1));
                admin.setName(rs.getString(2));
                admin.setPwd(rs.getString(3));
                list.add(admin);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }


    /**
     * 删除
     * @param delete
     * @return
     */
    @Override
    public int delete(String delete) {
        int count=0;
        String sql ="delete from enter where id=?";
        Object[] pame={delete};
        count=super.executeUpdate(sql,pame);
        if(count>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        return count;
    }

    @Override
    public int update(Admin admin) {
        int count=0;
        String sql="update enter set name=?,pwd=? where id=?";
        Object[] pama={admin.getName(),admin.getPwd(),admin.getId()};
        count=super.executeUpdate(sql,pama);
        if(count>0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
        return count;
    }
}
